package com.bianguo.android.apushdemo.utils;

import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/16.
 */

public class XmlUtil {

    public static String map2Xml(HashMap<String, String> map) {
        String xmlStr = "";
        try {
            XmlSerializer xml = XmlPullParserFactory.newInstance().newSerializer();
            StringWriter sw = new StringWriter();
            xml.setOutput(sw);
            xml.startDocument("utf-8", true);
            Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                xml.startTag("", entry.getKey());
                xml.text(entry.getValue());
                xml.endTag("", entry.getKey());
            }
            xml.endDocument();
            xmlStr = sw.toString();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return xmlStr;
    }
}
